/*
 * Copyright 2015 William Wang.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package wxk.Bonus.Network;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import wxk.Bonus.Constant.DBContant;
import wxk.Bonus.Constant.GlobalConfig;
import wxk.Bonus.Model.GenenalBonusModel;
import wxk.Bonus.bean.ChinaDoubleBallBean;
import wxk.Bonus.database.DBFactory;

/**
 * Jan 28, 20159:51:38 AM
 * @author William Wang
 */
public class UnionLottoSync extends javafx.concurrent.Task 
implements GeneralBonus
{
public  static ObservableList<ChinaDoubleBallBean> datalist=FXCollections.observableArrayList();
static GlobalConfig globalconfig=GlobalConfig.getInstance();

  
    @Override
    public void download(String url) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public GenenalBonusModel lastTerm() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public GenenalBonusModel getTerm(String term) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public boolean hasContinuum() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Vector<Integer> getContinuum() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected ObservableList<ChinaDoubleBallBean> call() throws Exception {
        DBFactory dbfactory = new DBFactory();
        int count=0;
        if(datalist!=null && datalist.size()>0)
        {
 System.out.println("before datalist size:"+datalist.size());            
        datalist.clear();
System.out.println("after datalist size:"+datalist.size());        
        }
        try( Connection conn=dbfactory.getConnection();
             Statement stmt =conn.createStatement();
             ResultSet rset=stmt.executeQuery(DBContant.READALL_UNION);
             )
        {
         while(rset !=null && rset.next())
         {
             ChinaDoubleBallBean row=
                     new ChinaDoubleBallBean(rset.getString("term"),
                     rset.getString("day"),
                     rset.getInt("red1"),
                     rset.getInt("red2"),
                     rset.getInt("red3"),
                     rset.getInt("red4"),
                     rset.getInt("red5"),
                     rset.getInt("red6"),
                     rset.getInt("blue")
             );
           count++;
           if(count%DBContant.SLEEP_COUNT==0)
           {
     globalconfig.updateSystemStatusBar("deal data..."+count);
//               Thread.sleep(DBContant.SLEEP_COUNT);
           }
          datalist.add(row);
//          Thread.sleep(1);
         }
        }catch (Exception e)
         {
           e.printStackTrace();
         }
     System.out.println("datalist size:"+datalist.size());   
     globalconfig.updateSystemStatusBar("Read history data..."+datalist.size());
     globalconfig.stopWorking();
        return datalist;
    }

    @Override
    public int clear() {
        int result=0;
        DBFactory dbfactory = new DBFactory();

        try( Connection conn=dbfactory.getConnection();
             Statement stmt =conn.createStatement();
          )
        {
        result=stmt.executeUpdate(DBContant.DELETEALL_UNION);     
        }catch(Exception e)
        {
         e.printStackTrace();
        }
        return result;
    }

}
